<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{padding: 0;margin: 0;}
        
        .box{
            width: 400px;
            height: 400px;
            background-color: grey;
            margin: 50px;
            overflow: hidden;
            /* position: relative; */
        }

        .sub{
            width: 300px;
            height: 300px;
            background-color: #6ff;
            margin: 50px;
            overflow: hidden;
            position: relative;
        }

        .txt{
            width: 100px;
            height: 100px;
            background-color: red;
            margin: 100px;
        }
       
    </style>
</head>
<body>
    <div class="box">
        <div class="sub">
            <div class="txt"></div>
        </div>
    </div>
</body>
<script>
    var box = document.getElementsByClassName("box")[0];
    var sub = document.getElementsByClassName("sub")[0];
    var txt = document.getElementsByClassName("txt")[0];
    console.dir(box);
    
    // 获取元素相对于父元素/包含块(定位)的偏移

    //  2. 
    // offParent    获取/查看参照物父元素
    // offsetLeft   offsetTop   左偏移/上偏移       => 获取元素相对于参照物父元素的偏移 (margin和定位)
    // console.log(box.offsetLeft);
    // console.log(box.offsetTop);

    // 如何确定参照物父元素? 
    // 自目标元素的父元素开始逐层向上一级查找,如果父元素上有定位相关的属性(relative,absolute,fixed,sticky),该父元素就是参照物父元素,否则继续向上一层查找, 依次类推, 都找不到 body就是参照物父元素

    console.log(txt.offsetParent,txt.offsetLeft,txt.offsetTop);


    // 如果txt参照物父元素是 sub , 但是我想获取txt相对于body的偏移:






</script>
</html>